package com.onenine.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.onenine.app.R;
import com.onenine.app.event.SingleInputEvent;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.utils.Toasty;

import org.greenrobot.eventbus.EventBus;

/**
 * 创建时间：2018/5/22 0022 19:15
 * 创建人：fly
 * 功能描述：单行信息采集框
 */
public class SingleInputActivity extends BaseActivity {

    private View mBtnDelete;
    private EditText mEdtInput;
    private SingleInputEvent mInputEvent;

    public static void launchActivity(Activity activity, SingleInputEvent inputEvent) {
        Intent intent = new Intent(activity, SingleInputActivity.class);
        intent.putExtra("data", inputEvent);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_special_sign;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mBtnDelete = findViewById(R.id.btn_delete);
        mEdtInput = findViewById(R.id.edt_input);
        mToolbar.setRightTitle(R.string.save);
        mToolbar.setRightTitleColor(RoleResLoader
                .getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
    }

    @Override
    protected void initListeners() {
        mToolbar.setOnRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mInputEvent.latestResult = mEdtInput.getText().toString();
                if (TextUtils.isEmpty(mInputEvent.latestResult)) {
                    Toasty.showNormal(R.string.content_not_empty_hint);
                    return;
                }
                EventBus.getDefault().post(mInputEvent);
                finish();
            }
        });
        mEdtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mBtnDelete.setVisibility(TextUtils.isEmpty(editable.toString()) ? View.GONE : View.VISIBLE);
            }
        });
        findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEdtInput.setText("");
            }
        });
    }

    @Override
    protected void initData() {
        mInputEvent = (SingleInputEvent) getIntent()
                .getSerializableExtra("data");
        if (mInputEvent == null) {
            throw new IllegalArgumentException("参数有误");
        }
        mToolbar.setCenterTitle(mInputEvent.title);
        mEdtInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputEvent.maxLength)});
        mEdtInput.setText(mInputEvent.preResult);
        mEdtInput.setSelection(mInputEvent.preResult.length());
        mEdtInput.setHint(mInputEvent.titleHint);
    }

}
